<?php

namespace app\common\model;

use app\model\AdminPayOrder;
use app\model\AdminRechargeRecord;
use app\util\BEIBEIGFUtil;
use think\facade\Log;

/**
 * 支付订单
 * Class PayOrderModel
 * @package app\common\model
 */
class PayOrderModel extends AdminPayOrder
{
    /**
     * @notes: 水电充值
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function waterElecCharge($payOrder,$staff)
    {
        $res = BEIBEIGFUtil::consumerCashRecharge($payOrder->device_no, $payOrder->money/100);
        if (!isset($res['businessnumber'])) {
            $data = [
                'order_status'       => 2,
                'pay_time'       => time(),
                'notes' => '支付成功，充值失败('.$res['msg'].')'
            ];
            AdminPayOrder::update($data, ['id' => $payOrder->id]);
            Log::error('充值处理订单号->'.$payOrder->order_no.'，充值失败('.$res['msg'].')');
            return false;
        }
        $data = [
            'order_status'       => 1,
            'trx_id'      => $res['businessnumber'],
            'pay_time'       => time(),
            'notes' => '充值成功'
        ];
        AdminPayOrder::update($data, ['id' => $payOrder->id]);
        // 写入充值记录
        $rechargeRecordData = [
            'staff_id'       => $staff->id,
            'school_id'       => $staff->school_id,
            'order_type'       => $payOrder->order_type,
            'order_no'  => $payOrder->order_no,
            'money'        => $payOrder->money,
            'recharge_time'  => time()
        ];
        AdminRechargeRecord::create($rechargeRecordData);
        Log::error('充值处理订单号->'.$payOrder->order_no.'，充值成功');
    }

}